/*
 * @ModuleName:
 * @Author: zhuzhaofeng
 * @Date: 2020-05-08 21:33:37
 */
/**
 * Array List 转 Tree Array
 * @param { String } jsonArr 需要转换的Array
 * @param { String } selfIdField 自身id字段名称
 * @param { String } parentIdField 父级id字段名称
 * @param { String } parentld 最顶级父级id
 */
export const array2Tree = (
  jsonArr = [],
  selfIdField = "id",
  parentIdField = "parentId",
  parentld = ""
) => {
  let result = [];
  for (const val of jsonArr) {
    if (val[parentIdField] === parentld) {
      let obj = {};
      for (const key in val) {
        obj[key] = val[key];
      }
      let children = array2Tree(
        jsonArr,
        selfIdField,
        parentIdField,
        obj[selfIdField]
      );
      obj.children = children;
      result.push(obj);
    }
  }
  return result;
};

export const uuid = () => {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
    let r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
};
